<!-- 
    右键菜单 文本复制
    linxp
    2021.03.03
-->
<template>
    <div class="textarea-copy" v-if="content">
        <dg-scrollbar>
            <p @contextmenu.prevent.stop="handleRightClick($event)" v-html="content"></p>
            <div class="right-popover" ref="popover" :style="position" v-show="isShow">
                <span
                    :class="{ active: item['value'] == currCopyType }"
                    v-for="(item, idx) in yslxOption"
                    :key="idx"
                    @click="handleChoose(item)"
                    >{{ item["label"] }}</span
                >
            </div>
        </dg-scrollbar>
    </div>
</template>
<script>
export default {
    name: "textarea-copy",
    props: {
        content: {
            type: String,
            default:
                ""
        },
        yslxMap: {
            type: Object,
            default() {
                return {};
            }
        }
    },
    data() {
        return {
            isShow: false,
            position: {
                left: 0,
                top: 0
            },
            // 复制文本
            text: "",
            currCopyType: "" // 当前鼠标选中内容所属的要素类型，默认为空
        };
    },
    computed: {
        yslxOption() {
            // 要素类型下拉框
            let result = [];

            if (!_.isEmpty(this.yslxMap)) {
                for (let key in this.yslxMap) {
                    let result_item = {
                        value: key,
                        label: this.yslxMap[key]
                    };
                    result.push(result_item);
                }
            }

            return result;
        }
    },
    methods: {
        /**
         * 选择类型
         * @param {string} item 当前类型
         */
        handleChoose(item) {
          
            this.$emit("getValue", {
                value: item.value,
                label: item.label,
                oldVal: this.currCopyType,
                text: this.text
            });
            // 重置当前选中类型
            if (this.currCopyType) {
                this.currCopyType = "";
            }
            this.isShow = false;
        },
        /**
         * 右键复制 弹窗菜单事件
         */
        handleRightClick(e) {
            // 获取所选文案
            let copyTxt = window.getSelection().toString();
            if (copyTxt.length > 1) {
                this.text = copyTxt;
                // 设置弹窗位置
                this.position.left = e.pageX + "px";
                this.position.top = e.pageY + "px";
                this.isShow = true;
                // 设置当前选中类型
                this.currCopyType = e.target.getAttribute("data-type") || "";
            }
        },

        /**
         * 外部点击事件
         * 注册非当前元素点击事件，关闭当前元素
         */
        handleClickOutSide(e) {
            if (this.$refs.popover) {
                let isSelf = this.$refs.popover.contains(e.target);
                if (!isSelf) {
                    this.isShow = false;
                }
            }
        }
    },
    created() {
        document.addEventListener("click", this.handleClickOutSide);
    },
    beforeDestroy() {
        document.removeEventListener("click", this.handleClickOutSide);
    }
};
</script>
<style lang="scss" scoped>
.textarea-copy {
    padding: 16px;
    border: 1px dashed rgba(24, 144, 255, 0.5);
    height: 245px;

    p {
        padding: 10px;
        background: rgb(246, 251, 255);
        text-align: justify;
        font-size: 14px;
        color: rgba(0, 0, 0, 0.65);
    }

    /deep/ .match-str {
        display: inline-block;
        /*background: #fdb933;*/
        color: #1890ff;
        background-color: #ddeefe;

        &.checked {
            color: rgba(0, 0, 0, 0.65);
            background-color: rgba(253, 185, 51, 0.7);
            cursor: pointer;
        }
    }
}

.right-popover {
    position: fixed;
    top: 0;
    left: 0;
    display: flex;
    flex-direction: column;
    background: #fff;
    min-width: 120px;
    border-radius: 4px;
    border: 1px solid #ebeef5;
    z-index: 2000;
    color: #606266;
    text-align: justify;
    font-size: 14px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    word-break: break-all;

    span {
        line-height: 36px;
        cursor: pointer;
        padding: 0 12px;

        &:hover,
        &.active {
            color: #1890ff;
            background-color: rgb(221, 238, 254);
        }
    }
}
</style>
